package com.gzy.literature.dao;

import com.gzy.literature.entity.UserFavorites;
import org.apache.ibatis.annotations.*;
import org.springframework.data.domain.Pageable;

import java.util.List;
import java.util.Date;

public interface UserFavoritesDao {

    /**
     * 通过ID查询单条数据
     *
     * @param userId 主键
     * @return 实例对象
     */
    @Select("SELECT * FROM user_favorites WHERE user_id = #{userId}")
    @Results(id = "userFavoritesResultMap", value = {
            @Result(property = "userId", column = "user_id"),
            @Result(property = "workId", column = "work_id"),
            @Result(property = "favoriteTime", column = "favorite_time")
    })
    UserFavorites queryById(String userId);

    /**
     * 查询指定条件的数据（用于分页）
     *
     * @param userFavorites 查询条件（可以为空，用于动态查询）
     * @return 对象列表
     */
    @Select("<script>" +
            "SELECT * FROM user_favorites WHERE 1=1 " +
            "<if test='userFavorites.userId != null'> AND user_id = #{userFavorites.userId} </if>" +
            "<if test='userFavorites.workId != null'> AND work_id = #{userFavorites.workId} </if>" +
            "</script>")
    @Results(id = "userFavoritesResultMapList", value = {
            @Result(property = "userId", column = "user_id"),
            @Result(property = "workId", column = "work_id"),
            @Result(property = "favoriteTime", column = "favorite_time")
    })
    List<UserFavorites>  queryDll(@Param("userFavorites") UserFavorites userFavorites);

    /**
     * 统计总行数
     * @param userFavorites 查询条件（可以为空，用于动态查询）
     * @return 总行数
     */
    @Select("<script>" +
            "SELECT COUNT(*) FROM user_favorites WHERE 1=1 " +
            "<if test='userFavorites.userId != null'> AND user_id = #{userFavorites.userId} </if>" +
            "<if test='userFavorites.workId != null'> AND work_id = #{userFavorites.workId} </if>" +
            "</script>")
    long count(@Param("userFavorites") UserFavorites userFavorites);

    /**
     * 新增数据
     *
     * @param userFavorites 实例对象
     * @return 影响行数
     */
    @Insert("INSERT INTO user_favorites(user_id, work_id, favorite_time) VALUES(#{userId}, #{workId}, #{favoriteTime})")
    @Options(useGeneratedKeys = true, keyProperty = "userId", keyColumn = "user_id")
    int insert(UserFavorites userFavorites);
    /**
     * 修改数据
     *
     * @param userFavorites 实例对象
     * @return 影响行数
     */
    @Update("<script>" +
            "UPDATE user_favorites " +
            "<set>" +
            "<if test='userFavorites.workId != null'> work_id = #{userFavorites.workId}, </if>" +
            "<if test='userFavorites.favoriteTime != null'> favorite_time = #{userFavorites.favoriteTime}, </if>" +
            "</set>" +
            "WHERE user_id = #{userFavorites.userId}" +
            "</script>")
    int update(UserFavorites userFavorites);

    /**
     * 通过主键删除数据
     *
     * @param userId 主键
     * @return 影响行数
     */
    @Delete("DELETE FROM user_favorites WHERE user_id = #{userId}")
    int deleteById(String userId);
}